/*
 * author      : dormi330
 * date        : 2018/7/16
 * description : TODO
 */

package com.deep.trade.adaptor.required.mq.nats;

import io.nats.client.Connection;
import io.nats.client.Nats;
import io.nats.client.Options;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class NatsClient {

    private Logger log = LoggerFactory.getLogger(this.getClass().getName());

    @Value("${mq.url}")
    private String mqUrl;

    @Bean
    public Connection initConnection() throws IOException {
        Options options = new Options.Builder()
                .errorCb(ex -> log.error("Connection Exception: ", ex))
                .disconnectedCb(event -> log.error("Channel disconnected: {}", event.getConnection()))
                .reconnectedCb(event -> log.error("Reconnected to server: {}", event.getConnection()))
                .build();

        return Nats.connect(mqUrl, options);
    }

}
